/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    long prev = LONG_MIN;
    bool isValidBST(TreeNode* root) {
        return dfs(root);
    }

    bool dfs(TreeNode* node)
    {

        if (node == nullptr) return true;


        bool left = dfs(node->left);
        if (left == false) return false;
        if (node->val <= prev)
        {
            return false;
        }
        prev = node->val;
        bool right = dfs(node->right);

        if (left && right) return true;
        else return false;
    }
};/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    long prev = LONG_MIN;
    bool isValidBST(TreeNode* root) {
        return dfs(root);
    }

    bool dfs(TreeNode* node)
    {

        if (node == nullptr) return true;


        bool left = dfs(node->left);
        if (left == false) return false;
        if (node->val <= prev)
        {
            return false;
        }
        prev = node->val;
        bool right = dfs(node->right);

        if (left && right) return true;
        else return false;
    }
};/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    long prev = LONG_MIN;
    bool isValidBST(TreeNode* root) {
        return dfs(root);
    }

    bool dfs(TreeNode* node)
    {

        if (node == nullptr) return true;


        bool left = dfs(node->left);
        if (left == false) return false;
        if (node->val <= prev)
        {
            return false;
        }
        prev = node->val;
        bool right = dfs(node->right);

        if (left && right) return true;
        else return false;
    }
};/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    long prev = LONG_MIN;
    bool isValidBST(TreeNode* root) {
        return dfs(root);
    }

    bool dfs(TreeNode* node)
    {

        if (node == nullptr) return true;


        bool left = dfs(node->left);
        if (left == false) return false;
        if (node->val <= prev)
        {
            return false;
        }
        prev = node->val;
        bool right = dfs(node->right);

        if (left && right) return true;
        else return false;
    }
};/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    long prev = LONG_MIN;
    bool isValidBST(TreeNode* root) {
        return dfs(root);
    }

    bool dfs(TreeNode* node)
    {

        if (node == nullptr) return true;


        bool left = dfs(node->left);
        if (left == false) return false;
        if (node->val <= prev)
        {
            return false;
        }
        prev = node->val;
        bool right = dfs(node->right);

        if (left && right) return true;
        else return false;
    }
};